import {
  createRouter,
  createWebHistory, type NavigationGuardNext,
  type RouteLocationNormalized,
  type RouteRecordNormalized,
  type Router
} from 'vue-router'
import {getToken, getUser} from "@/config"
//系统视窗组件
import AboutView from "@/views/system/About/Index.vue";
import LoginView from "@/views/system/Login/Index.vue"
import RegisterView from "@/views/system/Register/Index.vue"
import ForgetView from "@/views/system/Forget/Index.vue"
//用户视窗组件
import studentRouter from "@/router/student";
//管理员视窗组件
import AdminLoginView from "@/views/Admin/Login/Index.vue"
import adminRouter from "@/router/admin";
//中间件
import MiddRouter from '@/router/midd'
//响应状态组件
import S403State from "@/components/states/S403State.vue";
import S404State from "@/components/states/S404State.vue";
import S500State from "@/components/states/S500State.vue";
import DemoView from "@/views/DemoView.vue";
import {message} from "ant-design-vue";

//路由设置
const router:Router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'App',
      redirect: () => '/about',
      meta: {requiresAuth: false, roles: ['*']},
    },
    {
      path: '/about',
      name: 'About',
      component: AboutView,
      meta: {requiresAuth: false, roles: ['*']},
    },
    {
      path: '/login',
      name: 'Login',
      component: LoginView,
      meta: {requiresAuth: false, roles: ['*']},
    },
    {
      path: '/register',
      name: 'Register',
      component: RegisterView,
      meta: {requiresAuth: false, roles: ['*']},
    },
    {
      path: '/forget',
      name: 'Forget',
      component: ForgetView,
      meta: {requiresAuth: false, roles: ['*']},
    },
      ...studentRouter,
    {
      path: '/admin/login',
      name: 'AdminLogin',
      component: AdminLoginView,
      meta: {requiresAuth: false, roles: ['*']},
    },
      ...adminRouter,
    {
      path: '/demo',
      name: 'Demo',
      component: DemoView,
      meta: {requiresAuth: false, roles: ['*']},
    },
      ...MiddRouter,
    {
      path: '/403',
      name: '403',
      component: S403State,
      meta: {requiresAuth: false, roles: ['*']},
    },
    {
      path: '/404',
      name: '404',
      component: S404State,
      meta: {requiresAuth: false, roles: ['*']},
    },
    {
      path: '/500',
      name: '500',
      component: S500State,
      meta: {requiresAuth: false, roles: ['*']},
    },
    {
      path: '/:catchAll(.*)',
      redirect: () => '/404',
    }
  ]
})
//全局路由守卫
router.beforeEach((to:RouteLocationNormalized,from:RouteLocationNormalized,next:NavigationGuardNext):void=>{
  //登录就有token
  const tokenbln:boolean = !!getToken()
  //如果用户不存在，则无法访问需要登录的页面
  const userStr:IUser|null = getUser()
  if(to.meta.requiresAuth && userStr){
    userStr.role = userStr.role === 1
        ?'admin'
        :'student'
  }
  //用户身份
  const userRule:any = ''
  //检查是否需要验证
  if(to.matched.some((record:RouteRecordNormalized) => record.meta.requiresAuth)){
    //需要验证，但是没有token，表示未登录
    if(!tokenbln || !userStr) {
      message.destroy()
      message.warning("- 请先登录 -")
      next(from.path)
    }else if(to.matched.some(record => record.meta.roles)){
      //检查用户是否符合满足路由的要求
      const hasRole:boolean = to.matched.some((recode:RouteRecordNormalized):boolean => {
        const roles:Array<string> =recode.meta.roles as Array<string>
        return roles.some((role:string):boolean => userStr.role ? userStr.role.includes(role) : false)
      })
      if(hasRole){
        next()
      }else {
        next('/403')
      }
    }
  }else {
    next()
  }
})
export default router
